﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EpiNEXT.Core;
using EpiNEXT.Core.Fields;

namespace EpiNEXT.WPF.View.Pages
{
    /// <summary>
    /// Interaction logic for Search.xaml
    /// </summary>
    public partial class Search : UserControl
    {
        public Search()
        {
            InitializeComponent();
            FirstFloor.ModernUI.Presentation.AppearanceManager.Current.DarkThemeCommand.Execute(this);
        }

        private FormViewModel SelectedForm { get; set; }

        private void dgRecords_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.PropertyName == "Id" || e.PropertyName == "IdString" || e.PropertyName == "Form" || e.PropertyName == "Error" ||
                e.PropertyName == "ForeignKey" || e.PropertyName == "RecStatus" || e.PropertyName == "FirstSaveLogOn" ||
                e.PropertyName == "ForeignKeyString" || 
                e.PropertyName == "FirstSaveTime" || e.PropertyName == "LastSaveLogOn" || e.PropertyName == "LastSaveTime")
            {
                e.Cancel = true;
            }
        }

        private void dgRecords_AutoGeneratedColumns(object sender, EventArgs e)
        {
            dgRecords.Columns.Clear();

            ProjectViewModel vm = this.DataContext as ProjectViewModel;
            FormViewModel formVM = vm.SelectedForm;

            foreach (DataField field in formVM.Fields.DataFields)
            {
                if (field is ImageField || field is DataGridField || field is FixedCellTextBoxField) { continue; }
                if (!(field is IInputField)) { continue; }

                System.Windows.Controls.DataGridTextColumn textColumn = new System.Windows.Controls.DataGridTextColumn();
                textColumn.IsReadOnly = true;
                textColumn.Binding = new Binding(field.Name);
                textColumn.Header = field.ColumnHeaderText;

                if (field.ColumnNameTranslations != null)
                {
                    string currentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();

                    foreach (FieldTextPropertyTranslation translation in field.ColumnNameTranslations)
                    {
                        if (currentCulture.Equals(translation.Culture))
                        {
                            textColumn.Header = translation.Text;
                            break;
                        }
                    }
                }

                if (field.ColumnHeaderWidth.HasValue && field.ColumnHeaderWidth.Value >= 0)
                {
                    textColumn.Width = field.ColumnHeaderWidth.Value;
                }
                dgRecords.Columns.Add(textColumn);
            }
        }

        private void dgRecords_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            bool newValue = (bool)e.NewValue;
            if (newValue == true)
            {
                dgRecords.IsSynchronizedWithCurrentItem = true;
                ProjectViewModel vm = this.DataContext as ProjectViewModel;
                FormViewModel formVM = vm.SelectedForm;

                //if (SelectedForm == null || SelectedForm != formVM)
                //{
                    SelectedForm = formVM;
                    dgRecords.Columns.Clear();

                    PageViewModel pageVM = formVM.PagesView.Cast<PageViewModel>().First();

                    foreach (IField iField in pageVM.Fields) //formVM.Fields.DataFields)
                    {
                        if (iField is DataField)
                        {
                            DataField field = iField as DataField;

                            if (field is ImageField || field is DataGridField || field is FixedCellTextBoxField) { continue; }
                            if (!(field is IInputField)) { continue; }

                            System.Windows.Controls.DataGridTextColumn textColumn = new System.Windows.Controls.DataGridTextColumn();
                            textColumn.IsReadOnly = true;
                            textColumn.Binding = new Binding(field.Name);
                            textColumn.Header = field.ColumnHeaderText;

                            if (field.ColumnNameTranslations != null)
                            {
                                string currentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();

                                foreach (FieldTextPropertyTranslation translation in field.ColumnNameTranslations)
                                {
                                    if (currentCulture.Equals(translation.Culture))
                                    {
                                        textColumn.Header = translation.Text;
                                        break;
                                    }
                                }
                            }

                            if (field.ColumnHeaderWidth.HasValue && field.ColumnHeaderWidth.Value >= 0)
                            {
                                textColumn.Width = field.ColumnHeaderWidth.Value;
                            }
                            dgRecords.Columns.Add(textColumn);
                        }
                    }
                //}
            }
            else
            {
                dgRecords.IsSynchronizedWithCurrentItem = false;
                dgRecords.Columns.Clear();
                ProjectViewModel vm = this.DataContext as ProjectViewModel;
                vm.SelectedForm.SearchString = String.Empty;
                vm.SelectedForm.SearchRecords.Execute(null);
            }
        }
    }
}
